package zjh_20230327;

import zjh_20230310.Collection;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ClassUtil {
    static List<Field> fields1 = new ArrayList<>();
    static List<Method> methods1 = new ArrayList<>();

    public static List<Field> getAllFields(Class OldPerson) {
        Field[] fields = OldPerson.getDeclaredFields();

        for (Field f : fields) {
            fields1.add(f);
        }
        Class<?> superclass = OldPerson.getSuperclass();
        if (superclass != Object.class) {
            getAllFields(superclass);
        }

        return fields1;
    }

    public static List<Method> getAllMethods(Class OldPerson) {
        Method[] methods = OldPerson.getDeclaredMethods();
        for (Method m :
                methods) {
            methods1.add(m);
        }
        Class<?> superclass = OldPerson.getSuperclass();
        if (superclass != Object.class) {
            getAllMethods(superclass);
        }
        return methods1;
    }

    public static void main(String[] args) {
        OldPerson oldPerson = new OldPerson();
        List<Field> allFields = getAllFields(oldPerson.getClass());
        //迭代器
//        Iterator<Field> iterator = allFields.iterator();
//        while (iterator.hasNext()){
//            Field next = iterator.next();
//            System.out.println(next);
//        }
        for (Field af : allFields) {
            System.out.println("属性：" + af);
        }

        List<Method> allMethods = getAllMethods(oldPerson.getClass());
        for (Method aM : allMethods) {
            System.out.println("方法：" + aM);
        }
    }
}
